public class BotClient extends Client {

	// private boolean isBidding;
	private StrategyManager manager;

	BotClient() {
		super();
		manager = new StrategyManager();
	}

	private void createShutdownHook() {
		try {
			Runtime.getRuntime().addShutdownHook(new Thread() {
				public void run() {

					this.interrupt();
					quit();
					close();
				}
			});

			System.out.println("[Main thread] Shutdown hook added");
		} catch (Throwable t) {
			// we get here when the program is run with java
			// version 1.2.2 or older
			System.out.println("[Main thread] Could not add Shutdown hook");
		}
	}

	@Override
	public void listen() {

		createShutdownHook();
		String response = "";
		while (((response = this.getLine()) != null) && (isOn())) {
			if (response.startsWith("~")) {

				System.out.println(response);

				String parts[] = response.split(" ");

				switch (parts[0]) {
				case "~TURN":
					// out.println(Strategy.play);
					// or out.println(Strategy.draw);
					if (manager.isBidding())
						sendLine(manager.play());
					else
						sendLine(manager.draw());

					break;
				case "~HAND":
					// HAND [type] [score]
					try {
						int handType = Integer.parseInt(parts[1]);
						int handScore = Integer.parseInt(parts[2]);
						manager.setHandType(handType);
						manager.setHandScore(handScore);
					} catch (NumberFormatException e) {
						System.out.println("Error reading HAND");
						System.exit(0);
					}
					break;
				case "~PHASE":
					// isBidding=true;
					try {
						int phase = Integer.parseInt(parts[1]);
						manager.changePhase(phase);
					} catch (NumberFormatException e) {
						System.out.println("Error reading PHASE");
						System.exit(0);
					}
					break;
				case "~POSITION":
					try {
						int position = Integer.parseInt(parts[1]);
						manager.setPosition(position);
					} catch (NumberFormatException e) {
						System.out.println("Error reading POSITION");
						System.exit(0);
					}
					break;
				case "~NEXT":
					try {
						synchronized(this){
						wait(2000);
						}
						if (!manager.doIStay()) {
							this.sendLine("Q");
						} else {
							manager.nextRound();
						}
					} catch (NumberFormatException e) {
						System.out.println("Error reading NEXT");
						System.exit(0);
					} catch (InterruptedException e2) {
						System.out.println("Error reading NEXT");
						System.exit(0);
					}
					break;
				case "~HIGHEST":
					try {
						int highestBet = Integer.parseInt(parts[1]);
						manager.setHighestBet(highestBet);
					} catch (NumberFormatException e) {
						System.out.println("Error reading HIGHEST");
						System.exit(0);
					}
					break;
				case "~CASH":
					try {
						int cash = Integer.parseInt(parts[1]);
						manager.setCash(cash);
					} catch (NumberFormatException e) {
						System.out.println("Error reading CASH");
						System.exit(0);
					}
					break;
				case "~PLAYED":
					// PLAYED [id] [action]
					// 0 - fold, 1 - raise, 2 - call, 3 - check

					try {
						int id = Integer.parseInt(parts[1]);
						int action = Integer.parseInt(parts[2]);

						manager.played(id, action);
					} catch (NumberFormatException e) {
						System.out.println("Error reading PLAYED");
						System.exit(0);
					}

					break;
				case "~PARAMS":
					// PARAMS [maxPlayers] [initCash] [myId]

					try {
						int maxPlayers = Integer.parseInt(parts[1]);
						int initCash = Integer.parseInt(parts[2]);
						//int myId = Integer.parseInt(parts[3]);

						manager.setup(maxPlayers, initCash);
					} catch (NumberFormatException e) {
						synchronized (this) {
							System.out.println("Error reading PARAMS");
							System.exit(0);
						}
					}

					break;

				case "~ID":
					try {
						int id = Integer.parseInt(parts[1]);

						manager.setId(id);
					} catch (NumberFormatException e) {
						synchronized (this) {
							System.out.println("Error reading ID");
							System.exit(0);
						}
					}
					break;

				case "~RESULT":
					// RESULT [id] [type]

					try {
						int id = Integer.parseInt(parts[1]);
						int type = Integer.parseInt(parts[2]);
						manager.result(id, type);
					} catch (NumberFormatException e) {

					}

					break;

				case "~LEFT":
					// LEFT [playerId]
					try {
						int playerId = Integer.parseInt(parts[1]);
						manager.left(playerId);
					} catch (NumberFormatException e) {

					}

					break;
				default:
					continue;
				}

			} else {
				System.out.println(response);
				if (response.indexOf("***Bye") != -1) {
					System.exit(0);
				}

			}
		}

	}

	public static void main(String args[]) {
		BotClient client = new BotClient();
		client.getData();
		client.connect();
		client.listen();
	}

}
